package com.example.oasystem.controller.notepaper;

import com.alibaba.druid.sql.ast.statement.SQLIfStatement;
import com.example.oasystem.dao.notepaper.AoaNotepaperDao;
import com.example.oasystem.entity.notepaper.AoaNotepaper;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.user.AoaUser;
import net.sf.jsqlparser.statement.select.First;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class NotpaperController {
    @Autowired
    AoaNotepaperDao aoaNotepaperDao;
    @RequestMapping("writep")
    public String writep(String notepaperId,String title,String concent, HttpServletRequest request, Model model){
        System.out.println(notepaperId);
        System.out.println(title);
        System.out.println(concent);
        if (notepaperId.equals(""))
        {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            AoaNotepaper aoaNotepaper=new AoaNotepaper();
            aoaNotepaper.setTitle(title);
            aoaNotepaper.setConcent(concent);
            aoaNotepaper.setCreateTime(df.format(new Date()));
            AoaUser aoaUser=(AoaUser)request.getSession().getAttribute("user");
            aoaNotepaper.setNotepaperUserId(aoaUser.getUserId());
            aoaNotepaperDao.insertSelective(aoaNotepaper);
        }
        else
        {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            AoaNotepaper aoaNotepaper=new AoaNotepaper();
            aoaNotepaper.setTitle(title);
            aoaNotepaper.setConcent(concent);
            aoaNotepaper.setCreateTime(df.format(new Date()));
            aoaNotepaper.setNotepaperId(Long.valueOf(notepaperId));
            aoaNotepaperDao.updateByPrimaryKeySelective(aoaNotepaper);
        }

        return "forward:/userpanel";
    }
    @RequestMapping("notepaper")
    public String notepaper(String id, HttpServletRequest request, @RequestParam("page") int page, Model model){
        System.out.println(id);
        System.out.println("page = " + page);
        aoaNotepaperDao.deleteByPrimaryKey(Long.valueOf(id));

        return "forward:/userpanel?page="+page;
    }
    @RequestMapping("userpanel")
    public String userpanel(HttpServletRequest request,@RequestParam(value = "page", required = false,defaultValue = "0") int number,Model model){
        AoaUser aoaUser=(AoaUser)request.getSession().getAttribute("user");
        List<AoaNotepaper> quan=aoaNotepaperDao.selectQuan(aoaUser.getUserId());
        int totalElement= quan.size();
        int size=5;
        int totalPages=totalElement%size==0? totalElement/size:totalElement/size+1;
        boolean first;
        if (number==0)
        {
            first=true;
        }
        else
        {
            first=false;
        }
        boolean last;
        if (number==(totalPages-1))
        {
            last=true;
        }
        else
        {
            last=false;
        }
        Page page=new Page(totalElement,size,totalPages,number, first,last);
        List<AoaNotepaper> list=aoaNotepaperDao.selectAll((number)*size,size,aoaUser.getUserId());
        model.addAttribute("notepaperlist",list);
        System.out.println(page);
        model.addAttribute("page",page);
        model.addAttribute("url","panel");
        return "user/userpanel";
    }
    @RequestMapping("panel")
    public String panel(int page,HttpServletRequest request,Model model){
        AoaUser aoaUser=(AoaUser)request.getSession().getAttribute("user");
        List<AoaNotepaper> quan=aoaNotepaperDao.selectQuan(aoaUser.getUserId());
        int totalElement= quan.size();
        int size=5;
        int totalPages=totalElement%size==0? totalElement/size:totalElement/size+1;
        int number=page;
        boolean first;
        boolean last;
        if (number==0)
        {
             first=true;
        }
        else
        {
             first=false;
        }

        if (number==(totalPages-1))
        {
            last=true;
        }
        else
        {
            last=false;
        }
        Page fenye=new Page(totalElement,size,totalPages,number, first,last);
        List<AoaNotepaper> list=aoaNotepaperDao.selectAll((number)*size,size,aoaUser.getUserId());
        model.addAttribute("notepaperlist",list);
        System.out.println(fenye);
        model.addAttribute("page",fenye);
        model.addAttribute("url","panel");
        return "user/panel";
    }
}
